<template>
  <div class="checkout-address">
    <div class="text">
      <div class="none" v-if="!showAddress">
        您需要先添加收货地址才可提交订单。
      </div>
      <ul v-if="showAddress">
        <li>
          <span>收<i />货<i />人：</span>{{ showAddress.receiver }}
        </li>
        <li><span>联系方式：</span>{{ showAddress.contact }}</li>
        <li>
          <span>收货地址：</span
          >{{
            showAddress.fullLocation.replace(/ /g, "") + showAddress.address
          }}
        </li>
      </ul>
      <a
        href="javascript:;"
        v-if="showAddress"
        @click="addressEdit(showAddress)"
        >修改地址</a
      >
    </div>
    <div class="action">
      <click-btn class="btn" @click="changeAddress">切换地址</click-btn>
      <click-btn class="btn" @click="addressEdit({})">添加地址</click-btn>
    </div>
  </div>

  <!-- 切换地址 -->
  <x-dialog title="切换收货地址" v-model:visible="visible">
    <template #body>
      <div style="max-height: 450px; overflow: auto">
        <div
          class="text item"
          v-for="item in list"
          :key="item.id"
          @click="selectedAddress = item"
          :class="{ active: selectedAddress && selectedAddress.id === item.id }"
        >
          <ul>
            <li>
              <span>收<i />货<i />人：</span>{{ item.receiver }}
            </li>
            <li><span>联系方式：</span>{{ item.contact }}</li>
            <li>
              <span>收货地址：</span
              >{{ item.fullLocation.replace(/ /g, "") + item.address }}
            </li>
          </ul>
        </div>
      </div>
    </template>
    <template #footer>
      <click-btn type="gray" style="margin-right: 20px" @click="visible = false"
        >取消</click-btn
      >
      <click-btn type="primary" @click="confirmAddress">确认</click-btn>
    </template>
  </x-dialog>
  <!-- 添加与修改组件 -->
  <address-edit ref="addressEditCom" @onSuccess="successChange"></address-edit>
</template>
<script>
import { ref } from "vue";
import AddressEdit from "./AddressEdit";

export default {
  components: { AddressEdit },
  name: "CheckoutAddress",
  props: {
    list: {
      type: Array,
      default: () => [],
    },
  },
  // 1. 在拥有根元素的组件中，触发自定义事件，有没有emits选项无所谓
  // 2. 如果你的组件渲染的代码片段，vue3.0规范，需要在emits中申明你所触发的自定义事件
  // 3. 提倡：你发了自定义事件，需要在emits选项申明下，代码可读性很高
  //简单来说这个时间是在该组件的组件里触发的，父元素下看这不是一个整体，要么添加根元素把这两个组件包裹起来，不产生代码片段，要么emits
  emits: ["change"],
  setup(props, { emit }) {
    //找到默认收货地址
    const showAddress = ref(null);
    const defaultAddress = props.list.find((item) => item.isDefault === 0);
    if (defaultAddress) {
      showAddress.value = defaultAddress;
    } else if (props.list.length) {
      //如果没有默认收货地址，则显示第一条收货地址
      showAddress.value = props.list[0];
    }
    //如果没有收货地址，则显示新建地址

    //通知父组件一个收货地址，可以为空
    emit("change", showAddress.value && showAddress.value.id);
    //显示消息框
    const visible = ref(false);
    //记录选中地址id
    const selectedAddress = ref(null);

    //切换地址
    const changeAddress = () => {
      visible.value = true;
      selectedAddress.value = null;
    };

    //添加地址
    const addressEditCom = ref(null);
    const addressEdit = (address) => {
      addressEditCom.value.open(address);
    };

    //确认
    const confirmAddress = () => {
      //显示改变
      showAddress.value = selectedAddress.value;
      //通知父组件
      emit("change", selectedAddress.value.id);
      visible.value = false;
    };

    //添加修改地址
    const successChange = (formData) => {
      const address = props.list.find((item) => item.id === formData.id);
      if (address) {
        //修改
        for (const key in address) {
          address[key] = formData[key];
        }
      } else {
        //添加
        //如果这样修改数组的内容，这是浅拷贝，formData一变，list就变
        // props.list.unshift(formData);
        //方法1：重新请求list
        //方法2：深拷贝
        const jsonStr = JSON.stringify(formData);
        props.list.unshift(JSON.parse(jsonStr));
      }
    };
    return {
      showAddress,
      visible,
      selectedAddress,
      confirmAddress,
      changeAddress,
      addressEdit,
      addressEditCom,
      successChange,
    };
  },
};
</script>
<style scoped lang="less">
.checkout-address {
  border: 1px solid #f5f5f5;
  display: flex;
  align-items: center;
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    .none {
      line-height: 90px;
      color: #999;
      text-align: center;
      width: 100%;
    }
    > ul {
      flex: 1;
      padding: 20px;
      li {
        line-height: 30px;
        span {
          color: #999;
          margin-right: 5px;
          > i {
            width: 0.5em;
            display: inline-block;
          }
        }
      }
    }
    > a {
      color: @xtxColor;
      width: 160px;
      text-align: center;
      height: 90px;
      line-height: 90px;
      border-right: 1px solid #f5f5f5;
    }
  }
  .action {
    width: 420px;
    text-align: center;
    .btn {
      width: 140px;
      height: 46px;
      line-height: 44px;
      font-size: 14px;
      &:first-child {
        margin-right: 10px;
      }
    }
  }
}
.xtx-dialog {
  .text {
    flex: 1;
    min-height: 90px;
    display: flex;
    align-items: center;
    &.item {
      border: 1px solid #f5f5f5;
      margin-bottom: 10px;
      cursor: pointer;
      &.active,
      &:hover {
        border-color: @xtxColor;
        background: lighten(@xtxColor, 50%);
      }
      > ul {
        padding: 10px;
        font-size: 14px;
        line-height: 30px;
      }
    }
  }
}
</style>